//
//  WCBPlaceholderTextView.m
//  WeChatBuniess
//
//  Created by Guo Rui on 2017/12/14.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "WCBPlaceholderTextView.h"

@interface WCBPlaceholderTextView()<UITextViewDelegate>
@property (strong, nonatomic) TFPlaceholderTextView *textView;
@property (strong, nonatomic) UIButton *inputCountLabel;
@end

@implementation WCBPlaceholderTextView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
        self.layer.borderWidth = 1;
        self.layer.cornerRadius = 5.f;
        self.layer.masksToBounds = YES;
        self.textView.delegate = self;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat x, y, w, h;
    CGFloat width = CGRectGetWidth(self.frame);
    CGFloat height = CGRectGetHeight(self.frame);
    CGFloat countLabelHeight = 40;
    {
        x = y = 15;
        w = width - 2 * x;
        h = height - 2 * y - countLabelHeight - 15;
        self.textView.frame = CGRectMake(x, y, w, h);
    }
    
    {
        w = 180;
        x = width - w - 15;
        h = countLabelHeight;
        y = height - 15 - h;
        self.inputCountLabel.frame = CGRectMake(x, y, w, h);
    }
}

#pragma mark - Public
- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    self.textView.placeholderLabel.text = placeholder;
}

- (NSString *)text
{
    return self.textView.text;
}

#pragma mark - Delegate
- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.markedTextRange == nil) {
        NSString *text = textView.text;
        if (text.length >= self.maxInput) {
            text = [text substringWithRange:NSMakeRange(0, self.maxInput)];
            textView.text = text;
            self.inputCountLabel.selected = YES;
        }
        else {
            self.inputCountLabel.selected = NO;
        }
        
        [self.inputCountLabel setTitle:[NSString stringWithFormat:@"%zd / %zd", text.length, self.maxInput] forState:UIControlStateNormal];
    }
}

#pragma mark - Getter
- (UIButton *)inputCountLabel
{
    if (!_inputCountLabel) {
        _inputCountLabel = [UIButton buttonWithType:UIButtonTypeCustom];
        [_inputCountLabel setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [_inputCountLabel setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        _inputCountLabel.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _inputCountLabel.titleLabel.font = [UIFont systemFontOfSize:18];
        [_inputCountLabel setTitle:[NSString stringWithFormat:@"0 / %zd", self.maxInput] forState:UIControlStateNormal];
        [self addSubview:_inputCountLabel];
    }
    return _inputCountLabel;
}

- (TFPlaceholderTextView *)textView
{
    if (!_textView) {
        _textView = [[TFPlaceholderTextView alloc] init];
        _textView.font = [UIFont systemFontOfSize:20];
        _textView.tintColor = [UIColor lightGrayColor];
        [self addSubview:_textView];
    }
    return _textView;
}

@end
